package com.activenetwork.smlite.testcase.events.createEvents.basics;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import com.activenetwork.custom.uielement.decorate.IdentifierFactory;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.verification.gson.FailureAssertion;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics009 extends BaseCase {
	
	static final Logger logger = LoggerFactory.getLogger(Basics009.class);
	
	@Test
	public void Verify_only_one_location_can_be_added_when_create_events(){
		loginBusiness.login();
		
		String searchContent = "saha";
		EventsSnippet.gotoAddEventPage();
		
		/// select the first address in the suggest list
		WebElement eventLocation = IdentifierFactory.createUIElement("EventBasicSubview", "eventLocation");
		eventLocation.clear();
		eventLocation.sendKeys(searchContent);
		
		// waiting for AJAX loading
		PageLoader.load(EventBasicSubview.class);
		
		// Get the suggest list
		By by = By.cssSelector(".autotest-lookup-EventBasicSubview-event-locationref-displaystring div.nexus-lookup div.dropdown ul>li");
		List<WebElement> existingLocations = webDriver.findElements(by);
		existingLocations.get(0).findElement(By.xpath("./a")).click();
		
		// select the 2nd address in the suggest list
		eventLocation.sendKeys(searchContent);
		PageLoader.load(EventBasicSubview.class);
		
		existingLocations = webDriver.findElements(by);
		existingLocations.get(existingLocations.size()-1).findElement(By.xpath("./a")).click();
		
		// verify there is only one location for selected
		List<WebElement> locations = webDriver.findElements(By.cssSelector(".autotest-lookup-EventBasicSubview-event-locationref-displaystring div.nexus-lookup div[data-lookup-identifier]"));
		if(locations.size()>1){
			EnvContext.failurePointTL.get().add(new FailurePoint(
					Basics009.class.getName(), 
					"Verify adding multiple locations", 
					"It is able to add multiple locations in one event"));
		}
		
		//logout
		loginBusiness.logout();
		FailureAssertion.pop(Basics009.class);
		
	}

}
